package ysq.xyg.web.controller;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.model.po.*;
import ysq.xyg.web.model.vo.*;
import ysq.xyg.web.service.*;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 网站phone端
 *
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@RestController
public class PhoneController {
    private static Logger log= LoggerFactory.getLogger(PhoneController.class);
    @Resource
    private WebLinkerService webLinkerService;
    @Resource
    private WebNavigationService webNavigationService;
    @Resource
    private WebTagTypeService webTagTypeService;
    @Resource
    private WebColumnTypeService webColumnTypeService;
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private WebArticleService webArticleService;
    @Resource
    private WebImagesService webImagesService;
    /**
     * 统一首页
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView("/mobile/index");
        modelAndView=setBaseNews(modelAndView);
        return modelAndView;
    }
    /**
     * 统一详情
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/descTitle")
    public ModelAndView descTitle(HttpServletRequest request,String articleId){
        ModelAndView modelAndView=new ModelAndView("/mobile/descTitle");
        modelAndView=setBaseNews(modelAndView);
        WebArticle webArticle=webArticleService.selectByPrimaryKey(articleId);
        modelAndView.addObject("webArticle",webArticle);
        return modelAndView;
    }
    /**
     * 统一详情
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/desc")
    public ModelAndView desc(HttpServletRequest request,String articleId){
        ModelAndView modelAndView=new ModelAndView("/mobile/desc");
        modelAndView=setBaseNews(modelAndView);
        WebArticle webArticle=webArticleService.selectByPrimaryKey(articleId);
        List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(webArticle.getColId(),BaseConstant.base_show_type_zs,""));
        int num=0;
        if(webArticle!=null&&webArticleList!=null&&webArticleList.size()>0){
            for(int i=0;i<webArticleList.size();i++){
                if(webArticle.getId().equals(webArticleList.get(i).getId())){
                    num=i;
                }
            }
        }
        modelAndView.addObject("num",num);
        modelAndView.addObject("webArticleList",webArticleList);
        modelAndView.addObject("webArticle",webArticle);
        return modelAndView;
    }
    /**
     * 统一列表
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/list")
    public ModelAndView list(HttpServletRequest request,String colFlag){
        ModelAndView modelAndView=new ModelAndView("/mobile/list");
        modelAndView=setBaseNews(modelAndView);
        modelAndView.addObject("webColumnType",webColumnTypeService.getWebColumnTypeByColFlag(colFlag));
        modelAndView.addObject("colFlag",colFlag);
        return modelAndView;
    }




    public ModelAndView setBaseNews(ModelAndView modelAndView){
        List<WebNavigation> webNavigationList=webNavigationService.acceptNavMenuList(new WebNavigationVo(BaseConstant.base_use_type_moblie));
        WebConfig webConfig=acceptWebConfig();
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("webLinkerList",webLinkerService.listLeft(new WebLinkerVo(BaseConstant.base_show_type_zs)));
        modelAndView.addObject("webNavigationList",webNavigationList);
        return modelAndView;
    }

    public WebConfig acceptWebConfig(){
        List<WebConfig> list=webConfigService.acceptList(new WebConfigVo());
        if(list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
